﻿using MqttAnalyze.Data.Common;
using MqttAnalyze.Data.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MqttAnalyze.Data.Dal
{
    public class BaseSugarDal
    {
        protected ISqlSugarClient queryDb;
        protected string connectStr;

        public int QueryDataFromSql(string sql, SugarParameter[] parameters, out DataTable queryDt)
        {
            var rtnCode = SqlErrorCode.KFail;
            queryDt = new DataTable();
            try
            {
                queryDt = queryDb.Ado.GetDataTable(sql, parameters);
                rtnCode = SqlErrorCode.KSuccess;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "<QueryDataFromSql>");
            }
            return rtnCode;
        }

        public int ExecuteSqlCommand(string sql, params SugarParameter[] parameters)
        {
            var rtnCode = SqlErrorCode.KFail;
            try
            {
                var rst = queryDb.Ado.ExecuteCommand(sql, parameters);
                if (rst >= 0)
                {
                    rtnCode = SqlErrorCode.KSuccess;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "<ExecuteSqlCommand>");
            }
            return rtnCode;
        }
    }
}
